clc;
clearvars;
rng('shuffle')
% rng('default')
TempDataFile='..\CodesV15NewRunForDoubleCheck\Data\TestAnalizeTempFile.mat';
TempDataFile2='Data\TestAnalizeTempFile.mat';
Fitter={'MLE','MAP'};
% KNNMethodes={'KNN'};
FeaturesIndex={ 1:39 %All Features Included
%                 1:29         % No Fitting Features
                [37,28,8 ,30,29,38,3 ,16,31,32,2 ,12,9 ,11,19,6 ];  % Subset1 FrwSlc All
                [11,28,29,14,19,7 ,2 ,4 ,3 ,10,8 ,5 ,9 ,12,6 ,21]}; % Subset3 FrwSlc FitExc
KNNMethodes={'\itk-nn (\wp)',...             'KNN ExcFit',...
             '\itk-nn (\wp_s_u_b_1)',...
             '\itk-nn (\wp_s_u_b_2)'};
KNNMethodNum=length(KNNMethodes);

%%
ReplaceChaseFlag=true;
if exist(TempDataFile2,'file')
    Yes_NO='No';
%     Yes_NO = questdlg({'Cash File Exist','Replace it?'});
    if strcmp(Yes_NO,'No') || strcmp(Yes_NO,'Cancel')
        ReplaceChaseFlag=false;
    end
end
if ReplaceChaseFlag
CD=cd;
cd ..\CodesV15NewRunForDoubleCheck
load(TempDataFile)
Train=GetDataset(true);
cd(CD);
K=68;
TrainPredictedValue=zeros(Train.SNum,KNNMethodNum);
for k=1:KNNMethodNum
    fprintf('\n KNN Method: %s 00.00%%',KNNMethodes{k})
    for s=1:Train.SNum
        Index=[1:s-1,s+1:Train.SNum]';
        KNNRegressor = KNNReg(Train.Features(Index,FeaturesIndex{k}),Train.Label(Index),'NumNeighbors',K,'RegresiorMethod','InverseDistance');
        TrainPredictedValue(s,k)=GetRegValue(KNNRegressor,Train.Features(s,FeaturesIndex{k}));
        fprintf('\b\b\b\b\b\b%05.2f%%',s/Train.SNum*100);
    end
end
save(TempDataFile2,'Test','PredictedValue','Train','TrainPredictedValue')
else
load(TempDataFile2)
end

%%
BasePlotFileName='Plots\TestDataAnalize1_Fig';
SavePlots=0;
TextSize=14;
%%
%%
FittngMethodsNum=size(Test.FittedW,2);
FMAE=zeros(FittngMethodsNum,Test.SetNum);
for f=1:FittngMethodsNum
for i=1:Test.SetNum
    Erorrs=Test.FittedW(:,f,i)-Test.Label(:,i);
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    FMAE(f,i)=mean(AE);
end
end

BestFittngMethodsNum=size(Test.BestFittedW,2);
BFMAE=zeros(BestFittngMethodsNum,Test.SetNum);
for f=1:BestFittngMethodsNum
for i=1:Test.SetNum
    Erorrs=Test.BestFittedW(:,f,i)-Test.Label(:,i);
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    BFMAE(f,i)=mean(AE);
end
end
EMAE=zeros(KNNMethodNum,Test.SetNum);
EDnM=zeros(KNNMethodNum,Test.SetNum);
EUpM=zeros(KNNMethodNum,Test.SetNum);
for k=1:KNNMethodNum
for r=1:Test.SetNum
    Erorrs=PredictedValue(:,r,k)-Test.Label(:,r);
    AE = abs(Erorrs);
    EMAE(k,r)=mean(AE);
    EDnM(k,r)=mean(AE(AE<=EMAE(k,r)));
    EUpM(k,r)=mean(AE(AE>=EMAE(k,r)));
end
end
%% R for Train Data set
WMLE=Train.Features(:,34);
WMAP=Train.Features(:,37);
R=zeros(6,1);
for k=1:3
    R(k)=corr(TrainPredictedValue(:,k),Train.Label);
end
R(4)=corr(WMLE,Train.Label);
R(5)=corr(WMAP,Train.Label);

LinearCombinationModel=fitlm([TrainPredictedValue,WMLE,WMAP],Train.Label,'linear');
WOut=feval(LinearCombinationModel,[TrainPredictedValue,WMLE,WMAP]);
R(6)=corr(WOut,Train.Label);

Methodes=KNNMethodes;
Methodes{4}='MLE';
Methodes{5}='MAP';
Methodes{6}='Linear Combination';
ResTabel=table('Size',[6,3],'VariableTypes',{'string','double','double'});
for i=1:6
    ResTabel{i,1}=string(Methodes{i});
    ResTabel{i,2}=R(i);
end
ResTabel.Properties.VariableNames={'Method','Train_R','Test_R'};
%% R for Test dataset
WMLE=Test.BestFittedW(:,1,1);
WMAP=Test.BestFittedW(:,2,1);
Rt=zeros(6,1);
for k=1:3
    Rt(k)=corr(PredictedValue(:,1,k),Test.Label(:,1));
end
Rt(4)=corr(WMLE,Test.Label(:,1));
Rt(5)=corr(WMAP,Test.Label(:,1));
WP=PredictedValue(:,1,:);
WP=reshape(WP,[],3);
WOut=feval(LinearCombinationModel,[WP,WMLE,WMAP]);
Rt(6)=corr(WOut,Test.Label(:,1));

for i=1:6
    ResTabel{i,3}=Rt(i);
end
display(ResTabel)


